package com.dotstone.chipism.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.HttpURL;
import com.dotstone.chipism.bean.MySocket;
import com.dotstone.chipism.dialog.SVProgressHUD;
import com.dotstone.chipism.util.ConvertUtil;
import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.KeyManager;
import com.dotstone.chipism.util.SPUtils;
import com.dotstone.chipism.util.SocketManager;
import com.dotstone.chipism.util.ToastShow;
import com.dotstone.chipism.util.Util;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;
import com.tiqiaa.ircode.impl.InfraredFetcher;
import com.tiqiaa.irdnasdk.IrDnaSdkHelper;
import com.tiqiaa.remote.IRemoteManager;
import com.tiqiaa.remote.entity.Infrared;
import com.tiqiaa.remote.entity.Key;
import com.tiqiaa.remote.entity.Remote;
import com.tiqiaa.remote.impl.RemoteManager;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
//import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class AddProjectorActivity extends BaseActivity {

	private LinearLayout mBackL;
	private TextView mStatusBar;
	private Button mPowerB;
	private TextView mTitleTv;
	private boolean power = false;
	private Button mVoiceB;
	private Button mReturnB;
	private Button mBigB;
	private Button mSmallB;
	private Button mOKB;
	private Button mUpB;
	private Button mDownB;
	private Button mLeftB;
	private Button mRightB;
	private Button mSoundDownB;
	private Button mSoundUpB;
	private Button mMenuB;
	private Button mMoreB;
	private Remote remote;
	private int type;
	private String aplliance;
	private String brand;
	private String model;
	private String remoteId;
	private List<Key> keys = new ArrayList<Key>();
	private List<com.dotstone.chipism.bean.Key> localKeys = new ArrayList<com.dotstone.chipism.bean.Key>();
	private List<com.dotstone.chipism.bean.Key> moreKeys = new ArrayList<com.dotstone.chipism.bean.Key>();
	private List<Button> buttons = new ArrayList<Button>();
	private HashMap<Integer, String> cmdMaps = new HashMap<Integer, String>();
	private HashMap<Integer, String> irDataMaps = new HashMap<Integer, String>();
	private MySocket socket;
	private Button mSaveB;
	private SVProgressHUD mSVProgressHUD;
	private Vibrator vibrator;
	private Dialog dialog;
	private GridView mKeysG;
	private KeyAdapter mAdapter;

	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_layout:
			finish();
			break;
		case R.id.power_b:
			if (power) { // 当前打开 发送关指令
				mPowerB.setBackgroundResource(R.drawable.kaiguan22x);
				if (cmdMaps.get(4000) == null || cmdMaps.get(4000) == "") {
					sendCmd(cmdMaps.get(800));
				} else {
					sendCmd(cmdMaps.get(4000));
				}
				resetButton(false);
			} else { // 当前关闭 发送开的指令
				mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
				sendCmd(cmdMaps.get(800));
				resetButton(true);
			}
			power = !power;
			vibrator.vibrate(50);
			break;
		case R.id.voice_btn:
			ToastShow.Show(getApplicationContext(), "尚未实现");
			break;
		case R.id.return_btn:
			// 返回 806
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(806));
			break;
		case R.id.big_btn:
			// 返回 813
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(813));
			break;
		case R.id.small_btn:
			// 返回 814
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(814));
			break;
		case R.id.ok_btn:
			// 菜单OK 817
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(817));
			break;
		case R.id.up_btn:
			// 菜单上翻 818
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(818));
			break;
		case R.id.down_btn:
			// 菜单下翻 819
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(819));
			break;
		case R.id.left_btn:
			// 菜单左翻 820
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(820));
			break;
		case R.id.right_btn:
			// 菜单右翻 821
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(821));
			break;
		case R.id.sound_down_btn:
			// 音量- 810
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(810));
			break;
		case R.id.sound_up_btn:
			// 音量+ 809
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(809));
			break;
		case R.id.menu_btn:
			// 菜单 822
			vibrator.vibrate(50);
			sendCmd(cmdMaps.get(822));
			break;
		case R.id.more_btn:
			// ToastShow.Show(getApplicationContext(), "尚无更多按键");
			initDialog();
			break;
		case R.id.nextstep:
			save();
			break;
		default:
			break;
		}
	}

	@Override
	public void initParms(Bundle parms) {
		// TODO Auto-generated method stub

	}

	@Override
	public View bindView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_projector;
	}

	private void initDialog() {
		// TODO Auto-generated method stub
		dialog = new Dialog(AddProjectorActivity.this, R.style.MyDialogStyle);
		dialog.setContentView(R.layout.dialog_more_key);
		Window dialogWindow = dialog.getWindow();
		mKeysG = (GridView) dialog.findViewById(R.id.key_gv);
		mAdapter = new KeyAdapter();
		mKeysG.setAdapter(mAdapter);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = LayoutParams.FILL_PARENT;
		dialogWindow.setAttributes(lp);
		dialog.show();
	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub
		mBackL = $(R.id.back_layout);
		mStatusBar = $(R.id.tv_statusBar_mian);
		mTitleTv = $(R.id.title_tv);
		mPowerB = $(R.id.power_b);
		mVoiceB = $(R.id.voice_btn);
		mReturnB = $(R.id.return_btn);
		mBigB = $(R.id.big_btn);
		mSmallB = $(R.id.small_btn);
		mOKB = $(R.id.ok_btn);
		mUpB = $(R.id.up_btn);
		mDownB = $(R.id.down_btn);
		mLeftB = $(R.id.left_btn);
		mRightB = $(R.id.right_btn);
		mSoundDownB = $(R.id.sound_down_btn);
		mSoundUpB = $(R.id.sound_up_btn);
		mMenuB = $(R.id.menu_btn);
		mMoreB = $(R.id.more_btn);
		mSaveB = $(R.id.nextstep);
		mSaveB.setText(getString(R.string.save));
		vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
		type = getIntent().getIntExtra("type", 999);
		aplliance = getIntent().getStringExtra("appliance");
		brand = getIntent().getStringExtra("brand");
		model = getIntent().getStringExtra("model");
		remoteId = getIntent().getStringExtra("remoteId");
		mTitleTv.setText(aplliance + " " + brand + " " + model);
		socket = SocketManager.getInstance().sMap.get(DeviceManager
				.getInstance().getmWlanId());
		if (socket == null) {
			ToastShow.Show(getApplicationContext(), "请重新选择网关再添加！");
			// return;
			finish();
		}
		mSaveB.setVisibility(View.VISIBLE);
		IRemoteManager manager = new RemoteManager();
		remote = manager.getRemoteByID(remoteId);
		initKey();
		boolVersion();
		mBackL.setOnClickListener(this);
		mReturnB.setOnClickListener(this);
		mPowerB.setOnClickListener(this);
		mVoiceB.setOnClickListener(this);
		mBigB.setOnClickListener(this);
		mSmallB.setOnClickListener(this);
		mMenuB.setOnClickListener(this);
		mSoundDownB.setOnClickListener(this);
		mSoundUpB.setOnClickListener(this);
		mUpB.setOnClickListener(this);
		mDownB.setOnClickListener(this);
		mLeftB.setOnClickListener(this);
		mRightB.setOnClickListener(this);
		mOKB.setOnClickListener(this);
		mMoreB.setOnClickListener(this);
		mSaveB.setOnClickListener(this);
	}

	private void initKey() {
		keys = remote.getKeys();
		for (int i = 0; i < keys.size(); i++) {
			List<Infrared> infrareds = null;
			infrareds = new InfraredFetcher(this).fetchInfrareds(remote,
					keys.get(i));
			if (infrareds != null) {
				for (Infrared ir : infrareds) {
					if (ir != null && ir.getData() != null) {
						// int freq = ir.getFreq();
						// Required ！ 必须调用，此接口将对信号格式进行验证，返回明码信号数据
						// Log.i("wmy", "ir.getData() = "+ir.getData());
						byte[] data = IrDnaSdkHelper.getIrCode(ir.getFreq(),
								ir.getData());
						// byte[] airData =
						// IrDnaSdkHelper.getAirVMCode(keys.get(i).getProtocol());
						// data格式详见《信号数据格式说明》文档，不同的芯片需要转换成对应的芯片目标码
						// String str = byte2HexStr(data);
						String str = Util.bytesToHexString(data).toUpperCase();
						String cmd = ConvertUtil.getInstance()
								.convertInfraredCmd(str);
						// Log.w("wmy",
						// "send....###..解析出的红外数据..data = "+cmd+"         " +
						// Arrays.toString(data));
//						Log.e("wmy", keys.get(i).getType() + " " + cmd);
						cmdMaps.put(keys.get(i).getType(), cmd);
						irDataMaps.put(keys.get(i).getType(), str);
						com.dotstone.chipism.bean.Key k = new com.dotstone.chipism.bean.Key();
						k.setKeyNum(keys.get(i).getType());
						k.setCodeValue(cmd);
						localKeys.add(k);
					}
				}
			}
		}

		initLocalKeys();
	}

	private void resetButton(boolean e) {
		if (e) {
			initLocalKeys();
			mMoreB.setClickable(true);
		} else {
			for (int i = 0; i < buttons.size(); i++) {
				setBg(buttons.get(i).getId(), false);
			}
			mMoreB.setClickable(false);
			mMoreB.setBackgroundResource(R.drawable.ddd12x);
		}
	}

	private void initLocalKeys() {
		buttons.clear();
		buttons.add(mReturnB);
		buttons.add(mOKB);
		buttons.add(mMenuB);
		buttons.add(mUpB);
		buttons.add(mDownB);
		buttons.add(mLeftB);
		buttons.add(mRightB);
		buttons.add(mBigB);
		buttons.add(mSmallB);
		buttons.add(mSoundDownB);
		buttons.add(mSoundUpB);
		moreKeys.clear();
		// keys = device.getKeys();
		for (int i = 0; i < buttons.size(); i++) {
//			Log.i("wmy",
//					"buttons.get(i).getTag() = "
//							+ Integer.parseInt(buttons.get(i).getTag()
//									.toString()));
			String cmd = cmdMaps.get(Integer.parseInt(buttons.get(i).getTag()
					.toString()));
			if (cmd == null) {
				setBg(buttons.get(i).getId(), false);
			} else {
				setBg(buttons.get(i).getId(), true);
			}
		}
		for (int i = 0; i < localKeys.size(); i++) {
			// 如果按键不是温度加和温度减 进行判断
			if (localKeys.get(i).getKeyNum() != 811
					&& localKeys.get(i).getKeyNum() != 812
					&& localKeys.get(i).getKeyNum() != 800
					&& localKeys.get(i).getKeyNum() != 4000) {
				boolean e = false;
				for (Button btn : buttons) {

					int tag = Integer.parseInt(btn.getTag().toString());
					if (tag == localKeys.get(i).getKeyNum()) {
						e = true;
						// moreKeys.add(keys.get(i));
						break;
					}
				}
				if (!e) {
					boolean e1 = false;
					for (int n = 0; n < moreKeys.size(); n++) {
						if (moreKeys.get(n).getKeyNum() == localKeys.get(i)
								.getKeyNum()) {
							e1 = true;
						}
					}
					if (!e1) {
//						Log.i("wmy", "moreKeys添加 = " + localKeys.get(i).getKeyNum()
//								+ " " + i);
						moreKeys.add(localKeys.get(i));
					}
				}
			}
		}
//		Log.i("wmy", "moreKeys.size() = " + moreKeys.size());
		if (moreKeys.size() > 0) {
			mMoreB.setBackgroundResource(R.drawable.selector_more1);
			mMoreB.setClickable(true);
			// initDialog();
		} else {
			mMoreB.setBackgroundResource(R.drawable.ddd12x);
			mMoreB.setClickable(false);
		}
	}

	private void setBg(int id, boolean e) {
		if (e) {
			switch (id) {
			case R.id.return_btn:
				mReturnB.setClickable(true);
				mReturnB.setBackgroundResource(R.drawable.selector_return);
				break;
			case R.id.big_btn:
				mBigB.setClickable(true);
				mBigB.setBackgroundResource(R.drawable.selector_big);
				break;
			case R.id.small_btn:
				mSmallB.setClickable(true);
				mSmallB.setBackgroundResource(R.drawable.selector_small);
				break;
			case R.id.menu_btn:
				mMenuB.setClickable(true);
				mMenuB.setBackgroundResource(R.drawable.selector_menu);
				break;
			case R.id.ok_btn:
				mOKB.setClickable(true);
				mOKB.setBackgroundResource(R.drawable.selector_ok);
				break;
			case R.id.up_btn:
				mUpB.setClickable(true);
				mUpB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.down_btn:
				mDownB.setClickable(true);
				mDownB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.left_btn:
				mLeftB.setClickable(true);
				mLeftB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.right_btn:
				mRightB.setClickable(true);
				mRightB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.sound_up_btn:
				mSoundUpB.setClickable(true);
				mSoundUpB.setBackgroundResource(R.drawable.selector_sound_up);
				break;
			case R.id.sound_down_btn:
				mSoundDownB.setClickable(true);
				mSoundDownB
						.setBackgroundResource(R.drawable.selector_sound_down);
				break;
			default:
				break;
			}
		} else {
			switch (id) {
			case R.id.return_btn:
				mReturnB.setClickable(false);
				mReturnB.setBackgroundResource(R.drawable.fanhui12x);
				break;
			case R.id.big_btn:
				mBigB.setClickable(false);
				mBigB.setBackgroundResource(R.drawable.fangda12x);
				break;
			case R.id.small_btn:
				mSmallB.setClickable(false);
				mSmallB.setBackgroundResource(R.drawable.suoxiao12x);
				break;
			case R.id.menu_btn:
				mMenuB.setClickable(false);
				mMenuB.setBackgroundResource(R.drawable.caidan1x);
				break;
			case R.id.ok_btn:
				mOKB.setClickable(false);
				mOKB.setBackgroundResource(R.drawable.okoo12x);
				break;
			case R.id.up_btn:
				mUpB.setClickable(false);
				mUpB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.down_btn:
				mDownB.setClickable(false);
				mDownB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.left_btn:
				mLeftB.setClickable(false);
				mLeftB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.right_btn:
				mRightB.setClickable(false);
				mRightB.setBackgroundResource(R.drawable.selector_transparent);
				break;
			case R.id.sound_up_btn:
				mSoundUpB.setClickable(false);
				mSoundUpB.setBackgroundResource(R.drawable.yinjia12x);
				break;
			case R.id.sound_down_btn:
				mSoundDownB.setClickable(false);
				mSoundDownB.setBackgroundResource(R.drawable.yinjian12x);
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doBusiness(Context mContext) {
		// TODO Auto-generated method stub

	}

	public void boolVersion() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
//			getWindow().addFlags(
//					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			// 改变titlebar的高度
			int statusbarHeight = BarUtils.getStatusBarHeight(this);
			RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) mStatusBar
					.getLayoutParams();
			layoutParams1.height = statusbarHeight;
			mStatusBar.setLayoutParams(layoutParams1);
			mStatusBar.setVisibility(View.VISIBLE);
		} else {
			mStatusBar.setVisibility(View.GONE);
		}
	}

	// 保存该遥控器
	private void save() {
		mSVProgressHUD = new SVProgressHUD(AddProjectorActivity.this);
		mSVProgressHUD.showWithStatus("正在保存");
		// 上传设备
		RequestParams params = new RequestParams(HttpURL.UPLOAD_DEVICE_URL);
		params.setAsJsonContent(true);
		JSONObject obj = new JSONObject();
		try {
			obj.put("classification", "4");
			obj.put("key", "10000");
			obj.put("deviceModel", remoteId);
			obj.put("deviceName", aplliance + " " + brand + " " + model);
			obj.put("deviceType", type + "");
			obj.put("deviceVersion", "3.1.0");
			obj.put("deviceWifiSSID", remoteId.equals("") ? "未知" : remoteId);
			obj.put("deviceId", "0");
			obj.put("productId", "4");
			obj.put("mainDeviceId", DeviceManager.getInstance().getmWlanId());
			obj.put("hid", DeviceManager.getInstance().hid);
			obj.put("key", "10000");
			obj.put("requestTime", "" + System.currentTimeMillis());
			obj.put("token",
					(String) SPUtils.get(getApplicationContext(), "token", ""));
			Log.d("test", " upload obj = " + obj.toString());
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		params.setBodyContent(obj.toString());
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				Log.e("test", "upload=" + result);
				try {
					JSONObject jsonObject = new JSONObject(result);
					String value = jsonObject.getString("resultCode");
					if (value.equals("1")) {
						String deviceId = jsonObject.getString("data");
						// 获取deviceId之后 上传所有按键
						uploadKeys(deviceId);
					} else {
						if (mSVProgressHUD.isShowing()) {
							mSVProgressHUD.dismiss();
						}
						String errorMsg = jsonObject.getString("errorMsg");
						ToastShow.Show(getApplicationContext(), "保存失败："
								+ errorMsg);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onCancelled(CancelledException arg0) {

			}

			@Override
			public void onError(Throwable arg0, boolean arg1) {

			}

			@Override
			public void onFinished() {

			}

		});
	}

	private void uploadKeys(String deviceId) {
		// 上传按键
		String buttonCmdS = "";
		String positionS = "";
		String nameS = "";
		for (int i = 0; i < keys.size(); i++) {
			if (cmdMaps.get(keys.get(i).getType()) != null) {
				buttonCmdS += irDataMaps.get(keys.get(i).getType()) + ",";
				positionS += keys.get(i).getType() + ",";
//				nameS+=KeyManager.getInstance().getKeyNameById((int)keys.get(i).getType())+",";
				nameS+=keys.get(i).getLocalName()+",";
			}
		}
		if (buttonCmdS.length() > 1) {
			buttonCmdS = buttonCmdS.substring(0, buttonCmdS.length() - 1);
		}
		if (positionS.length() > 1) {
			positionS = positionS.substring(0, positionS.length() - 1);
		}
		if (nameS.length() > 1) {
			nameS = nameS.substring(0, nameS.length() - 1);
		}
		RequestParams params = new RequestParams(HttpURL.UPLOAD_REMOTE_KEY_URL);
		params.setAsJsonContent(true);
		JSONObject obj = new JSONObject();
		try {
			obj.put("buttonCmdS", buttonCmdS);
			obj.put("key", "10000");
			obj.put("positionS", positionS);
			obj.put("deviceId", deviceId);
			obj.put("nameS", nameS);
			obj.put("requestTime", "" + System.currentTimeMillis());
			obj.put("token",
					(String) SPUtils.get(getApplicationContext(), "token", ""));
			Log.d("test", " upload obj = " + obj.toString());
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		params.setBodyContent(obj.toString());
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				Log.e("test", "upload=" + result);
				try {
					JSONObject jsonObject = new JSONObject(result);
					String value = jsonObject.getString("resultCode");
					if (value.equals("1")) {
						if (mSVProgressHUD.isShowing()) {
							mSVProgressHUD.dismiss();
						}
						ToastShow.Show(getApplicationContext(), "保存成功");
						finish();
					} else {
						if (mSVProgressHUD.isShowing()) {
							mSVProgressHUD.dismiss();
						}
						String errorMsg = jsonObject.getString("errorMsg");
						ToastShow.Show(getApplicationContext(), "保存失败："
								+ errorMsg);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onCancelled(CancelledException arg0) {

			}

			@Override
			public void onError(Throwable arg0, boolean arg1) {

			}

			@Override
			public void onFinished() {

			}

		});
	}

	private void sendCmd(String cmd) {
		if (socket.isLocalSuccess()) {
			socket.sendMqttData(cmd);
		} else {
			SocketManager.getInstance().sendRemoteData(socket.getMac(),
					DeviceManager.getInstance().getmWlanId(), cmd);
		}
	}

	class KeyAdapter extends BaseAdapter {
		HoldView holdView;
		private int imageResId;
		private int stringResId;

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return moreKeys.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return moreKeys.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View v, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (v == null) {
				v = LayoutInflater.from(AddProjectorActivity.this).inflate(
						R.layout.item_more_key, null);
				holdView = new HoldView();
				holdView.btn = (Button) v.findViewById(R.id.key_btn);
				v.setTag(holdView);
			} else {
				holdView = (HoldView) v.getTag();
			}
			final com.dotstone.chipism.bean.Key k = moreKeys.get(position);
			holdView.btn.setTag(k.getKeyNum());
			int content = KeyManager.getInstance().getContentTypeByKeyNum(
					k.getKeyNum());
			if (content == 0) {
				imageResId = KeyManager.getInstance().getDrawableResId(
						k.getKeyNum());
				holdView.btn.setBackgroundResource(imageResId);
			} else {
				stringResId = KeyManager.getInstance().getStringResId(
						k.getKeyNum());
				holdView.btn.setText(stringResId);
			}
			holdView.btn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Log.i("wmy", "点击了" + k.getKeyNum());
					int tag = Integer.parseInt(v.getTag().toString());
					sendCmd(cmdMaps.get(tag));
					vibrator.vibrate(50);
				}
			});

			return v;
		}
	}

	class HoldView {
		private Button btn;
	}

}
